require("../css/commCss/normalize.less");
require("../css/commCss/reset.less");
require("../css/commCss/property.less");
require("../css/commCss/basic.less");
require("../fonts/iconfont.css");
require("../css/login.less");

require("weui");
//axios
const axios = require("axios");

//toast
const weui = require("weui.js");

/* 获取元素 */
const phone = document.querySelector("#phone"); //手机号
const pwd = document.querySelector("#pwd"); //密码
const regBtn = document.querySelector("#regBtn"); //注册账号

/* 点击注册按钮，触发注册验证 */
regBtn.addEventListener("click", function () {
  const phoneVal = phone.value; //手机号
  const pwdVal = pwd.value; //密码

  if (!/^1[3-9][0-9]{9}$/.test(phoneVal)) {
    errMsg.textContent = "手机号错误";
    return;
  } else if (!/^[a-zA-Z]\w{5,11}$/.test(pwdVal)) {
    //以字母开头，后面跟字母，数字，下划线
    errMsg.textContent = "密码错误";
    return;
  } else {
    //清空错误信息
    errMsg.textContent = "";

    phone.value = pwd.value = "";

    axios
      .post("http://139.9.177.51:8099/users/login", {
        account: phoneVal,
        password: pwdVal,
      })
      .then(function (res) {
        if (res.data.status === 0) {
          //将用户id存入本地存储
          localStorage.setItem("userId", res.data.data.user.userId);
          weui.toast("登录成功", {
            duration: 1000,
            //className: 'custom-classname', //自定义类名
            callback: function () {
              //回调函数
              location.href = "./home.html";
            },
          });
        } else {
          weui.alert(res.data.msg);
        }
      });
  }
});
